package com.sunnybear.framework.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.Gravity;

import com.sunnybear.framework.tools.log.Logger;

import androidx.appcompat.widget.AppCompatTextView;

/**
 * <p>
 *  2018/8/22.
 */
public class FillOnlyTextView extends AppCompatTextView {

    public FillOnlyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int paddingStart = getPaddingStart();
        int paddingEnd = getPaddingEnd();
        int alignment = getGravity();
        TextPaint paint = getPaint();
        paint.setColor(getCurrentTextColor());
        paint.drawableState = getDrawableState();
        int viewWidth = getMeasuredWidth();
        String text = getText().toString();
        int lineY = getPaddingTop();
        lineY += getTextSize();
        Layout layout = getLayout();

        // layout.getLayout()在4.4.3出现NullPointerException
        if (layout == null) {
            return;
        }

        Paint.FontMetrics fm = paint.getFontMetrics();

        int textHeight = (int) (Math.ceil(fm.descent - fm.ascent));
        textHeight = (int) (textHeight * layout.getSpacingMultiplier() + layout
                .getSpacingAdd());

        int realHeight = (int) (getTextSize() + textHeight * (layout.getLineCount() - 1));
        switch (alignment) {
            case Gravity.CENTER:
            case Gravity.CENTER_VERTICAL:
            case Gravity.CENTER_VERTICAL | Gravity.START:
            case Gravity.CENTER_VERTICAL | Gravity.END:
                lineY = (int) ((getMeasuredHeight() - realHeight) / 2f + getTextSize());
                break;
            case Gravity.BOTTOM:
                lineY = (int) ((getMeasuredHeight() - realHeight) - getPaddingBottom() + getTextSize());
                break;
        }
        int offsetSize = 0;
        for (int i = 0; i < layout.getLineCount(); i++) {
            int lineStart = layout.getLineStart(i) + offsetSize;
            int lineEnd = Math.min(layout.getLineEnd(i) + offsetSize, text.length());
            float width = StaticLayout.getDesiredWidth(text, lineStart,
                    lineEnd, getPaint());
            Logger.d("zxl", "mViewWidth:" + viewWidth + ",width:" + width);
            int offset = 0;
            float realWidth = width;
            while (realWidth < viewWidth - paddingEnd - paddingStart && offset + lineEnd < text.length()) {
                offset++;
                realWidth = StaticLayout.getDesiredWidth(text, lineStart,
                        offset + lineEnd, getPaint());
                Logger.d("zxl", "test:" + realWidth);
            }
            if (realWidth > viewWidth - paddingEnd - paddingStart) {
                offset--;
            }
            offsetSize += offset;
            Logger.d("zxl", "offsetSize:" + offsetSize);
            String line = text.substring(lineStart, lineEnd + offset);
            switch (alignment) {
                case Gravity.CENTER:
                case Gravity.CENTER_HORIZONTAL:
                    canvas.drawText(line, (viewWidth - realWidth) / 2, lineY, paint);
                    break;
                case Gravity.CENTER_VERTICAL | Gravity.END:
                case Gravity.END:
                    canvas.drawText(line, viewWidth - realWidth - paddingEnd, lineY, paint);
                    break;
                default:
                    canvas.drawText(line, paddingStart, lineY, paint);
            }
            lineY += textHeight;
        }
    }
}
